package com.beta.user.auth.center.service.provider;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.beta.cat.service.RedissonService;
import com.beta.user.auth.center.api.response.AuthCorpInfoResp;
import com.beta.user.auth.center.api.service.IRPCAuthCorpInfoervice;
import com.beta.user.auth.center.service.constant.BusinessConstant;
import com.beta.user.auth.center.service.mapper.model.AuthCorpInfo;
import com.beta.user.auth.center.service.service.IAuthCorpInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author pcc
 * @description
 * @date 2023-06-29 16:27
 */
@Slf4j
@DubboService
@Service
public class RPCAuthCorpInfoImpl implements IRPCAuthCorpInfoervice {

    @Autowired
    private RedissonService redissonService;

    @Autowired
    private IAuthCorpInfoService authCorpInfoService;

    @Override
    public AuthCorpInfoResp queryByCorp(String corpId) {
        String cacheKey = String.format(BusinessConstant.AUTH_CORP_INFO_CACHE, corpId);
        AuthCorpInfo authCorpInfo = redissonService.get(cacheKey, AuthCorpInfo.class);
        if (null == authCorpInfo) {
            List<AuthCorpInfo> lists = authCorpInfoService.getBaseMapper().selectList(new LambdaQueryWrapper<AuthCorpInfo>().eq(AuthCorpInfo::getDelFlag, BusinessConstant.NORMAL_FLAG).eq(AuthCorpInfo::getCorpId, corpId));
            if (CollectionUtils.isEmpty(lists)) {
                return null;
            }
            authCorpInfo = lists.get(0);
            redissonService.setJson(cacheKey, authCorpInfo, TimeUnit.HOURS.toMillis(2));
        }
        AuthCorpInfoResp resp = new AuthCorpInfoResp();
        BeanUtils.copyProperties(authCorpInfo,resp);
        return resp;
    }

}
